package logic

import (
	"context"
	"fmt"
	"novel/model/mysql"

	"novel/novelrpc/internal/svc"
	"novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 小说喜欢添加
func (l *CollectAddLogic) CollectAdd(in *novelrpc.CollectAddRequest) (*novelrpc.CollectAddResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Where("id=?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("该用户不存在")
	}

	data := mysql.Collect{
		UserId:      in.UserId,
		BookId:      in.BookId,
		CollectName: in.CollectName,
	}
	mysql.DB.Create(&data)
	return &novelrpc.CollectAddResponse{
		CollectId: int64(data.ID),
	}, nil
}
